#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>

using namespace std;
using std::setw;

void errorMsg(int errorType);
void displayTable(double &loan, double &APR, int &years, double payment);

void getLoan(double &loan)
{
   cout << "Enter the loan amount: $";
   cin >> loan;
   while (loan <= 0)
   {
      errorMsg(1);
      cout << "Enter the loan amount: $";
      cin >> loan;
   }
      
   return;
}

void getAPR(double &APR)
{
   cout << "Enter the annual percentage interest rate: ";
   cin >> APR;
   while (APR <= 0)
   {
      errorMsg(2);
      cout << "Enter the annual percentage interest rate: ";
      cin >> APR;
   }
   if (APR > 1)
      APR = APR * .01;
   return;
}

void getYears(int &years)
{
   cout << "Enter the loan term (in years): ";
   cin >> years;
   return;
}


void getOption()
{
   cout << "\nOptions:"
        << "\n\t(n) : Enter new values"
        << "\n\t(d) : Display the amortization schedule"
        << "\n\t(q) : Quit";
   return;
}

void calculatePmt(double principal, float APR, int years, double& payment)
{
   payment = 0.0;
   payment = (principal * (APR / 12)) / (1 - pow(1 + (APR / 12.0),(-12.0 * years)));
   cout << "\nThe monthly mortgage payment is $";
   cout << payment << endl;
   return;
}


void interact(double &loan, double &APR, int &years, double& payment)
{
   char option;
   do
   {
      getOption();

      do
      {
         cout << "\n> ";
         cin >> option;
         switch (option)
         {
            case 'n':
               cout << endl;
               getLoan(loan);
               getAPR(APR);
               getYears(years);
               calculatePmt(loan, APR, years, payment);
               break;
            case 'd':
               cout << endl;
               displayTable(loan, APR, years, payment);
               break;
            case 'q':
               return;
               break;
         }
      }
      while (option != 'n'
         && option != 'd'
         && option != 'q');
   }
   while (option != 'q');
   return;      
}

void errorMsg(int errorType)
{
   cout << "\nERROR: ";
   
   switch (errorType)
   {
      case 1:
         cout << "Please enter a number greater than 0\n";
         break;
      case 2:
         cout << "Please enter a valid interest rate\n";
         break;
      case 3:
         break;
      case 4:
         break;
      case 5:
         break;
   }

   return;
}


void displayTable(double &principal, double &APR, int &years, double payment)
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   double newInterest = principal * (APR / 12);
   double newPrincipal = payment - newInterest;
   double balance = principal - newPrincipal;
   double totalInterest = newInterest;
   double totalPrincipal = newPrincipal;

   int months = years * 12;


   //ytd is year to date.
   cout << "Month   Principal   Interest   Ytd Principal   Ytd Interest    "
        << "Balance\n";
   cout << "-----   ---------   --------   -------------   ------------   "
        << "----------\n";
   for (int i = 1; i <= months; i++)
   {
      cout << setw(5) << i << setw(12) << newPrincipal << setw(11) << newInterest
           << setw(16) << totalPrincipal << setw(15) << totalInterest
           << setw(13) << balance << endl;
      newInterest = balance * (APR / 12);
      balance = balance - newPrincipal;
      newPrincipal = payment - newInterest;
      totalPrincipal = totalPrincipal + newPrincipal;
      totalInterest = totalInterest + newInterest;
   }
}

void MortgageAmortization()
{
   cout << "Welcome to Mortgage Amortization Calulator!!!" << endl;
   cout << "Programmed by Shermattie Brijram" << endl;
   cout << endl;
   
   double loan = 0.0;
   double APR = 0.0;
   int years = 0;
   int errorType = 0;
   double payment = 0.0;

   getLoan(loan);
   getAPR(APR);
   getYears(years);
   calculatePmt(loan, APR, years, payment);
   interact(loan, APR, years, payment);

   system ("PAUSE");
  }